package com.xunge.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunge.mapper.HomeworkMapper;
import com.xunge.pojo.Homework;
import com.xunge.pojo.HomeworkQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.service.HomeworkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class HomeworkServiceImpl implements HomeworkService {

    @Autowired
    private HomeworkMapper homeworkMapper;

    /**
     * 分页查询作业信息
     */
    @Override
    public PageResult<Homework> page(HomeworkQueryParam homeworkQueryParam) {
        // 设置分页参数
        PageHelper.startPage(homeworkQueryParam.getPage(), homeworkQueryParam.getPageSize());
        // 执行查询
        Page<Homework> page = (Page<Homework>) homeworkMapper.list(homeworkQueryParam);
        // 封装分页结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 新增作业
     */
    @Override
    public void save(Homework homework) {
        homework.setCreateTime(LocalDateTime.now());
        homework.setUpdateTime(LocalDateTime.now());
        homeworkMapper.insert(homework);
    }

    /**
     * 根据ID查询作业
     */
    @Override
    public Homework getById(Integer id) {
        return homeworkMapper.getById(id);
    }

    /**
     * 根据ID修改作业
     */
    @Override
    public void update(Homework homework) {
        homework.setUpdateTime(LocalDateTime.now());
        homeworkMapper.update(homework);
    }

    /**
     * 根据ID删除作业
     */
    @Override
    public void deleteById(Integer id) {
        homeworkMapper.deleteById(id);
    }

    /**
     * 批量删除作业
     */
    @Override
    public void delete(List<Integer> ids) {
        homeworkMapper.deleteByIds(ids);
    }

    /**
     * 根据课程ID查询作业
     */
    @Override
    public List<Homework> getByCourseId(Integer courseId) {
        return homeworkMapper.getByCourseId(courseId);
    }

    /**
     * 根据班级ID查询作业
     */
    @Override
    public List<Homework> getByClazzId(Integer clazzId) {
        return homeworkMapper.getByClazzId(clazzId);
    }

    /**
     * 根据教师ID查询作业
     */
    @Override
    public List<Homework> getByTeacherId(Integer teacherId) {
        return homeworkMapper.getByTeacherId(teacherId);
    }
} 